﻿using System;
using System.Collections.Generic;
using System.Linq;
using MemberShip.Imagine.Models.Helpers;

namespace TVXD23.Web.Models
{
    public class IMGRepository
    {
        public TVXD23Entities Db = new TVXD23Entities();
        public TVXD23Entities DB
        {
            get
            {
                return Db;
            }
        }
        public List<IMAGE> ImagesInAlbum(int albumId, int page)
        {
            var s = Db.ALBUMs.Where(m => m.ID == albumId).FirstOrDefault().IMAGEs.OrderByDescending(m => m.CreateDay);

            var mss = new PagedList<IMAGE>(s, page, 5).ToList();
            return mss;
        }
        public List<IMAGE> ImagesInAlbum(int albumId, int page,int spt)
        {
            var s = Db.ALBUMs.Where(m => m.ID == albumId).FirstOrDefault().IMAGEs.OrderByDescending(m => m.CreateDay);

            var mss = new PagedList<IMAGE>(s, page, spt).ToList();
            return mss;
        }

        public void SetSlide(int id)
        {
            var d = Db.IMAGEs.Where(m => m.ID == id).FirstOrDefault();
            if (d != null)
                d.IsSlide = true;
            Db.SaveChanges();
        }
        public bool IsSlide(int id)
        {
            var s = Db.IMAGEs.Where(m => m.ID == id).FirstOrDefault();
            if (s != null && s.IsSlide != null)
                return s.IsSlide.Value;
            else
            {
                return false;
            }
        }
        public void UnSetSlide(int id)
        {
            var d = Db.IMAGEs.Where(m => m.ID == id).FirstOrDefault();
            if (d != null)
                d.IsSlide = false;
            Db.SaveChanges();
        }
        public List<IMAGE> SlideImages()
        {
            return Db.IMAGEs.Where(m => m.IsSlide == true).ToList();
        }
        public List<ALBUM> FindAllAlbum()
        {
            return Db.ALBUMs.ToList();
        }

        public ALBUM FindAlbumById(int id)
        {
            return Db.ALBUMs.FirstOrDefault(m => m.ID == id);
        }

        public void DeleteAlbumById(int id)
        {
            Db.ALBUMs.DeleteObject(Db.ALBUMs.FirstOrDefault(m => m.ID == id));
            Db.SaveChanges();
        }

        public void CreateNewAlbum(ALBUM album)
        {
            Db.ALBUMs.AddObject(album);
            Db.SaveChanges();
        }

        public void EditAlbum(ALBUM oldalbum)
        {
            var newalbum = Db.ALBUMs.FirstOrDefault(m => m.ID == oldalbum.ID);
            if (newalbum == null) return;
            newalbum.AlbumName = oldalbum.AlbumName;
            newalbum.AlbumPath = oldalbum.AlbumPath;
            newalbum.Description = oldalbum.Description;
            Db.SaveChanges();
        }

        public string FindAlbumNameById(int albumid)
        {
            return Db.ALBUMs.FirstOrDefault(m => m.ID == albumid).AlbumName;
        }
        //images
        public List<IMAGE> FindAllImagesByAlbumId(int albumid)
        {
            var img = from p in Db.IMAGEs
                      join q in Db.ALBUMs on p.AlbumId equals q.ID
                      where q.ID == albumid
                      select p;
            return img.ToList();
        }

        public List<IMAGE> FindAllImages()
        {
            var img = from p in Db.IMAGEs
                      select p;
            return img.ToList();
        }

        public List<IMAGE> ViewImage(int spt)
        {
            return Db.IMAGEs.OrderByDescending(m => m.CreateDay).Take(spt).ToList();
        }

        public IMAGE FindImageById(int id)
        {
            return Db.IMAGEs.FirstOrDefault(m => m.ID == id);
        }

        public void CommitChange()
        {
            Db.SaveChanges();
        }


        public void DeleteImage(int id)
        {
            IMAGE dm = Db.IMAGEs.Where(m => m.ID == id).FirstOrDefault();
            Db.IMAGEs.DeleteObject(dm);
            Db.SaveChanges();
        }
    }
}